package com.quark.service.system;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quark.mapper.system.GenFormConfMapper;
import com.quark.mapper.system.GeneratorMapper;
import com.quark.model.system.gen.GenConfig;
import com.quark.model.system.gen.GenFormConf;
import com.quark.util.GenUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * @author 张高昌
 * @date 2022/1/26 18:26
 * @description: 代码生成器
 */
@Service
@RequiredArgsConstructor
public class GeneratorService {

    private final GenFormConfMapper genFormConfMapper;

    public IPage<Map<String, Object>> getPage(Page page, String tableName, String dsName) {
        GeneratorMapper mapper = GenUtils.getMapper(dsName);
        // 手动切换数据源
        DynamicDataSourceContextHolder.push(dsName);
        return mapper.queryTable(page, tableName);
    }

    /**
     * 预览代码
     *
     * @param genConfig
     * @return
     * @author 张高昌
     * @date 2022/1/29 15:14
     */
    public Map<String, String> previewCode(GenConfig genConfig) {
        // 根据tableName 查询最新的表单配置
        List<GenFormConf> formConfList = genFormConfMapper.selectList(Wrappers.<GenFormConf>lambdaQuery()
                .eq(GenFormConf::getTableName, genConfig.getTableName()).orderByDesc(GenFormConf::getCreateTime));
        String tableNames = genConfig.getTableName();
        String dsName = genConfig.getDsName();
        GeneratorMapper mapper = GenUtils.getMapper(genConfig.getDsName());
        for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) {
            // 查询表信息
            Map<String, String> table = mapper.queryTable(tableName, dsName);
            // 查询列信息
            List<Map<String, String>> columns = mapper.selectMapTableColumn(tableName, dsName);
            // 生成代码
            if (CollUtil.isNotEmpty(formConfList)) {
                return GenUtils.generatorCode(genConfig, table, columns, null, formConfList.get(0));
            } else {
                return GenUtils.generatorCode(genConfig, table, columns, null, null);
            }
        }

        return MapUtil.empty();
    }

    public byte[] generatorCode(GenConfig genConfig){
        // 根据tableName 查询最新的表单配置
        List<GenFormConf> formConfList = genFormConfMapper.selectList(Wrappers.<GenFormConf>lambdaQuery()
                .eq(GenFormConf::getTableName, genConfig.getTableName()).orderByDesc(GenFormConf::getCreateTime));

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);

        String tableNames = genConfig.getTableName();
        String dsName = genConfig.getDsName();
        GeneratorMapper mapper = GenUtils.getMapper(dsName);

        for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) {
            // 查询表信息
            Map<String, String> table = mapper.queryTable(tableName, dsName);
            // 查询列信息
            List<Map<String, String>> columns = mapper.selectMapTableColumn(tableName, dsName);
            // 生成代码
            if (CollUtil.isNotEmpty(formConfList)) {
                GenUtils.generatorCode(genConfig, table, columns, zip, formConfList.get(0));
            }
            else {
                GenUtils.generatorCode(genConfig, table, columns, zip, null);
            }
        }
        IoUtil.close(zip);
        return outputStream.toByteArray();
    }
}
